<?php
namespace cosocket;

class CoSocket
{

    final public static function autoLoader($class)
    {
        $path = explode('\\', $class);
        $base = __DIR__;
        $filename = $path[count($path) - 1] . '.php';
        $filepath = $base;
        foreach ($path as $key => $val)
        {
            if ($key == 0 || $key == count($path) - 1) {
                continue;
            }
            $filepath .= DIRECTORY_SEPARATOR . $val;
        }
        $filepath .= DIRECTORY_SEPARATOR . $filename;
        require_once $filepath;
    }

    public static function run()
    {
        date_default_timezone_set('Asia/Shanghai');
        ini_set('display_errors', 'On');
        error_reporting(E_ERROR); /*E_ALL,E_ERROR*/
        self::initDefine(); // 设置常量

        \spl_autoload_register(__CLASS__ . '::autoLoader');
        $config = require __DIR__ . '/config/server.php';

        $server = \cosocket\system\server\ServerFactory::create($config);
        $server->run();
    }

    private static function initDefine()
    {
        define('DS', DIRECTORY_SEPARATOR);
        define("ENABLE_COROUTINE", true);
        define('LOG_PATH', './logs'); //日志目录
        define('PHP_LOG_THRESHOLD', 1); //错误记录级别 ERROR=1, DEBUG=2, WARNING=3, NOTICE=4, INFO=5, ALL=6
        define("PROJ_PATH", realpath(dirname(__FILE__) . DS));
        define("APP_ROOT", PROJ_PATH . DS . "application");

        define("SERVER_ID", getenv('SERVER_ID')); // 服务器ID
        define('DEBUG', 1);  /* 是否调试  0-不打印调试日志  1-打印调试日志 */
    }
}

#启动服务器
require_once __DIR__ . '/system/Functions.php';
CoSocket::run();